import { WorksTagEntity } from '../../works-tag/entities/works-tag.entity';
import {
  Column,
  CreateDateColumn,
  Entity,
  JoinTable,
  ManyToMany,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';
import { WorksInfoDto } from '../dto/create-work.dto';

@Entity('works')
export class WorksEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ length: 50 })
  name: string;

  @Column({ type: 'text' })
  bgImage: string;

  @CreateDateColumn({
    type: 'timestamp',
    comment: '创建时间',
    name: 'create_time',
  })
  createTime: Date;

  @UpdateDateColumn({
    type: 'timestamp',
    comment: '更新时间',
    name: 'update_time',
  })
  updateTime: Date;

  @Column({ type: 'timestamp', name: 'start_time', default: null })
  startTime: Date;

  @Column({ type: 'timestamp', name: 'end_time', default: null })
  endTime: Date;

  @Column({ type: 'text', default: null })
  desc: string;

  @Column({ type: 'text', default: null })
  path: string;

  @Column({ type: 'text', default: null })
  git: string;

  @Column({ type: 'text', default: null })
  link: string;

  @Column('simple-enum', { enum: [0, 1] })
  status: number;

  @Column('simple-enum', { enum: [0, 1] })
  hot: number;

  //标签
  @ManyToMany(() => WorksTagEntity, (worksTag) => worksTag.workses)
  @JoinTable({
    name: 'works_worksTag',
    joinColumns: [{ name: 'works_id' }],
    inverseJoinColumns: [{ name: 'worksTag_id' }],
  })
  worksTags: WorksTagEntity[];

  toResponseObject(): WorksInfoDto {
    let responseObj: WorksInfoDto = this;
    // if (this.worksTags && this.worksTags.length) {
    //     responseObj.worksTags = this.worksTags.map((item) => item.name);
    //   }
    return responseObj;
  }
}
